﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.pressions;
using MyMES.Writer.API.Application.Commends.Works;

namespace MyMES.Writer.API.Application.CommendHandlers.Works
{
    public class DelestWorksHandler:IRequestHandler<DeleteWorksCommand,APIResult<WorkOrder>>
    {
        private readonly IBaseRepository<WorkOrder> repository;
        private readonly IMapper mapper;

        public DelestWorksHandler(IBaseRepository<WorkOrder> repository,IMapper mapper )
        {
            this.repository = repository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 删除生产工单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<APIResult<WorkOrder>> Handle(DeleteWorksCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<WorkOrder> result = new APIResult<WorkOrder>();
                result.Code = APIEnum.删除成功;
                result.Msg = "删除成功";
                var pro = mapper.Map<WorkOrder>(request);
                string sql = "update WorkOrder set IsDlete=true where WorkOrderId=@WorkOrderId";
                await repository.Update(pro, sql);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
         
        }
    }
}
